package v1

import (
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
	"zimuzu_cvn_web_api/router/api"
	"zimuzu_cvn_web_api/router/api/app/middleware"
	"zimuzu_cvn_web_api/service"
)

type IndexAPI struct {
}

func (index IndexAPI) Name() string {
	return "index API"
}

func (index IndexAPI) Bind() {
	indexAPI := config.C.Router.Group("/v1/app/index", middleware.AuthMiddleware())
	{
		indexAPI.GET("market", market)
		indexAPI.GET("msgCheck", indexMsgCheck)
	}
}

type apiConfirmParams struct {
	U string `form:"u"`
}

//主站接口二次确认
//func apiConfirm(c *gin.Context) {
//	var params apiConfirmParams
//	err := c.ShouldBindQuery(&params)
//	if err != nil {
//		api.SendResponse(c, errno.ParamsError, nil)
//		return
//	}
//	if rrapi.ApiConfirm(params.U) {
//		api.SendResponse(c, errno.OK, nil)
//	} else {
//		api.SendResponse(c, errors.New("确认异常"), nil)
//	}
//
//	return
//}

type priceResponseData struct {
	Status int    `json:"status"`
	Info   string `json:"info"`
	Data   struct {
		ETH     Price `json:"eth,omitempty"`
		BTC     Price `json:"btc,omitempty"`
		BTCBase Price `json:"btcBase,omitempty"`
		ETHBase Price `json:"ethBase,omitempty"`
	} `json:"data,omitempty"`
}
type Price struct {
	Change    float64 `json:"change"`
	Price     float64 `json:"price"`
	UsdPrice  float64 `json:"usdPrice"`
	CnyPrice  float64 `json:"cnyPrice"`
	DayVloume float64 `json:"volume"`
	High      float64 `json:"high"`
	Low       float64 `json:"low"`
}

//行情
//Auth: Xu
func market(c *gin.Context) {
	market := priceResponseData{}
	httpurl := config.C.JapenUrl + "/v1/market/getCvntPrice"
	if err := HttpGet(httpurl, &market); err != nil {
		api.SendResponse(c, errors.New("获取行情信息失败"), nil)
		return
	}
	var responseData []responseMarket
	responseData = append(responseData, marketInfoChange("CVNT", "ETH", market.Data.ETH))
	responseData = append(responseData, marketInfoChange("CVNT", "BTC", market.Data.BTC))
	responseData = append(responseData, marketInfoChange("BTC", "USDT", market.Data.BTCBase))
	responseData = append(responseData, marketInfoChange("ETH", "USDT", market.Data.ETHBase))

	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, responseData)
	return

}

//首页查询未读消息条数
//Auth: Xu
func indexMsgCheck(c *gin.Context) {
	uid, _ := service.GetUserIdByRequest(c)
	dealUnreadNum, otherDealUnreadNum, noticeUnreadNum, err := service.GetUnreadMessageNum(uid)
	if err != nil {
		api.SendResponse(c, err, nil)
		return
	}

	responseData := map[string]int{
		"msgNum": dealUnreadNum + otherDealUnreadNum + noticeUnreadNum,
	}
	errno.OK.Info = "查询成功"
	api.SendResponse(c, errno.OK, responseData)
	return
}

type responseMarket struct {
	Currency          string `json:"currency"`
	Counterparty      string `json:"counterparty"`
	Change            string `json:"change"`
	Price             string `json:"price"`
	CounterpartyPrice string `json:"counterpartyPrice"`
	Volume            string `json:"volume"`
	Lowest            string `json:"lowest"`
	Highest           string `json:"highest"`
}

func marketInfoChange(curr, baseCurr string, price Price) responseMarket {
	var change string
	if price.Change > 0 {
		change = "+" + fmt.Sprintf("%.2f", price.Change) + "%"
	} else {
		change = fmt.Sprintf("%.2f", price.Change) + "%"
	}
	highLowPoint := 2
	usdtPoint := 2
	if curr == "CVNT" {
		highLowPoint = 8
		usdtPoint = 3
	}

	return responseMarket{
		Currency:          curr,
		Counterparty:      baseCurr,
		Change:            change,
		Price:             strconv.FormatFloat(price.UsdPrice, 'f', usdtPoint, 64) + "USD",
		CounterpartyPrice: strconv.FormatFloat(price.Price, 'f', highLowPoint, 64),
		Volume:            strconv.FormatFloat(price.DayVloume, 'f', -1, 64),
		Highest:           strconv.FormatFloat(price.High, 'f', highLowPoint, 64),
		Lowest:            strconv.FormatFloat(price.Low, 'f', highLowPoint, 64),
	}
}
